import Util from "./js/util.js";
import SystemDemo from "./js/systemdemo.js";

//这个函数在整个wps加载项中是第一个执行的
function OnAddinLoad(ribbonUI) {
  if (typeof window.Application.ribbonUI != "object") {
    window.Application.ribbonUI = ribbonUI;
  }

  if (typeof window.Application.Enum != "object") {
    // 如果没有内置枚举值
    window.Application.Enum = Util.WPS_Enum;
  }

  //这几个导出函数是给外部业务系统调用的
  window.openOfficeFileFromSystemDemo = SystemDemo.openOfficeFileFromSystemDemo;
  window.InvokeFromSystemDemo = SystemDemo.InvokeFromSystemDemo;


  return true;
}
//cx读取本地附件token 、编辑新增状态
// wps.ApiEvent.AddApiEventListener("DocumentOpen", DocumentOpen())


// 数据初始化
function DocumentOpen() {
  // 获取本地token
  let tempPath = window.Application.Env.GetTempPath();
  if (wps.FileSystem.Exists(tempPath + '/token.txt')) {
    let token = window.Application.FileSystem.ReadFile(tempPath + '/token.txt')
    if (token) {
      window.Application.PluginStorage.setItem('token', token)
    }
  } else {
    console.log('文件不存在');
  }

  //获取是否新增编辑模板状态
  if (wps.FileSystem.Exists(tempPath + '/addoredit.txt')) {
    let editoradd = window.Application.FileSystem.ReadFile(tempPath + '/addoredit.txt');
    if (editoradd) {
      window.Application.PluginStorage.setItem('editoradd', editoradd);
    }
  } else {
    console.log('文件不存在');
  }
  setTimeout(() => {
    //设置默认菜单页
    if (wps.ribbonUI) {
      wps.ribbonUI.ActivateTab('wpsAddinTab')
    }
  }, 800)
  //设置默认菜单页

}

const menuList = [
  {
    id: "moBanGuanLi", //模板管理
    type: "taskpane",
  },
  {
    id: "yueTan", //智能约谈
    type: "taskpane",
  },
];

//  Ribbon按钮响应函数
function OnAction(control) {
  const eleId = control.Id;
  // 隐藏除了自己其他菜单内容
  // goHideMenuContent(eleId);
  switch (eleId) {
    // 模版管理
    case "moBanGuanLi":
      {
        // if (!tsId) {
        //   let tskpane = window.Application.CreateTaskPane(
        //     Util.GetUrlPath() + Util.GetRouterHash() + "/mobanguanli"
        //   );
        //   let id = tskpane.ID;
        //   window.Application.PluginStorage.setItem("moBanGuanLi", id);
        //   tskpane.DockPosition = window.Application.Enum.msoCTPDockPositionLeft
        //   tskpane.Visible = true;
        // } else {
        //   let tskpane = window.Application.GetTaskPane(tsId);
        //   tskpane.DockPosition = window.Application.Enum.msoCTPDockPositionLeft
        //   tskpane.Visible = !tskpane.Visible;
        // }
        let tsId = window.Application.PluginStorage.getItem("moBanGuanLi");
        if (tsId) {
          let tskpane = window.Application.GetTaskPane(tsId);
          tskpane.DockPosition = window.Application.Enum.msoCTPDockPositionLeft
          tskpane.Visible = false;
        }
        let tskpane = window.Application.CreateTaskPane(
          Util.GetUrlPath() + Util.GetRouterHash() + "/mobanguanli"
        );
        let id = tskpane.ID;
        window.Application.PluginStorage.setItem("moBanGuanLi", id);
        tskpane.DockPosition = window.Application.Enum.msoCTPDockPositionLeft
        tskpane.Visible = true;
      }
      break;
    // 模版管理
    case "yueTan":
      {
        let tsId = window.Application.PluginStorage.getItem("yueTan");
        if (!tsId) {
          let tskpane = window.Application.CreateTaskPane(
            Util.GetUrlPath() + Util.GetRouterHash() + "/yuetan"
          );
          let id = tskpane.ID;
          window.Application.PluginStorage.setItem("yueTan", id);
          tskpane.Visible = true;
        } else {
          let tskpane = window.Application.GetTaskPane(tsId);
          tskpane.Visible = !tskpane.Visible;
        }
      }
      break;
    default:
      break;
  }
  return true;
}

//隐藏菜单
function goHideMenuContent(eleId) {
  // debugger
  // let find = menuList.find(it => it.id == eleId)
  // let tsId = window.Application.PluginStorage.getItem("moBanGuanLi");
  let tskpane = null
  menuList.forEach(item => {
    if (item.id != eleId) {
      tskpane = window.Application.GetTaskPane(item.id);
      // tskpane.Visible = false
      if (tskpane) {
        tskpane.Visible = !tskpane.Visible
      }

    }
  })

}

//获取图片
function GetImage(control) {
  const eleId = control.Id;
  switch (eleId) {
    case "moBanGuanLi":
      return "images/moBanGuanLi.svg";
    case "yueTan":
      return "images/yueTan.svg";
    case "btnShowTaskPane":
      return "images/3.svg";
    default:
  }
  return "images/default.svg";
}
//是否禁用
function OnGetEnabled(control) {
  const eleId = control.Id;
  // true  不禁用  false 禁用
  switch (eleId) {
    case "moBanGuanLi":
      return true;
    case "yueTan": {
      return true;
    }
    default:
      break;
  }
  return true;
}
// 是否显示menu按钮
function OnGetVisible(control) {
  // const eleId = control.Id;
  // console.log(eleId);
  return true;
}

//这些函数是给wps客户端调用的
export default {
  OnAddinLoad,
  OnAction,
  GetImage,
  OnGetEnabled,
  OnGetVisible,
  menuList,
};
